﻿using System;
using System.Linq;
using AutoMapper;
using Fee;
using Fee.ViewModel;
using Identity.ViewModels;
using JwtWebApi.Data;

namespace JwtWebApi.Models.ViewModel
{
    public class OrderContractViewModel
    {
        public OrderContractViewModel()
        {
        }

        public OrderContractViewModel(OrderContract contract)
        {
            Id = contract.Id;
            ContractNo = contract.ContractNo;
            OrderNo = contract.OrderNo;
            Status = contract.Status;
            Amount = contract.Amount;
            FinishTime = contract.FinishTime;
            CreateTime = contract.SignTime;
            FrameworkContract = contract.FrameworkContract != null
                ? new FrameworkContractSimpleViewModel(contract.FrameworkContract)
                : null;
            Vendor = Mapper.Map<VendorDetailViewModel>(contract.Vendor);
            Project = contract.Project != null ? new ProjectSimpleViewModel(contract.Project) : null;
            Tax = contract.Tax;

            Name = contract.Name;
            AmountWithoutTax = contract.AmountWithoutTax;

            Comment = contract.Comment;
            Contact = contract.Contact;
            Tax = contract.Tax;
        }
       
        public decimal Tax { get; set; }
        public string Comment { get; set; }
        public decimal AmountWithoutTax { get; set; }
        public string Name { get; set; }

        /// <summary>
        /// </summary>
        public string OrderNo { get; set; }

        /// <summary>
        /// </summary>
        public string ContractNo { get; set; }

        public int Id { get; set; }

        public decimal Amount { get; set; }

        public ProjectSimpleViewModel Project { get; set; }

        public FrameworkContractSimpleViewModel FrameworkContract { get; set; }

        public ContractStatus Status { get; set; }

        public DateTimeOffset CreateTime { get; set; }

        public DateTimeOffset? FinishTime { get; set; }

        /// <summary>
        ///     供应商
        /// </summary>
        public VendorDetailViewModel Vendor { get; set; }


        /// <summary>
        ///     联系人
        /// </summary>
        public string Contact { get; set; }


        public OrderContract ToOrderContract(ApplicationDbContext context)
        {
            return new OrderContract
            {
                Id = Id,
                SignTime = CreateTime,
                Status = Status,
                FinishTime = FinishTime,
                Vendor = Vendor != null ? context.Vendors.FirstOrDefault(f => f.Id == Vendor.Id) : null,
                Name = Name,
                AmountWithoutTax = AmountWithoutTax,
                ContractNo = ContractNo,
                Contact = Contact,
                FrameworkContract = FrameworkContract != null
                    ? context.FrameworkContracts.FirstOrDefault(f => f.Id == FrameworkContract.Id)
                    : null,
                Amount = Amount,
                Tax = Tax,
                OrderNo = OrderNo
            };
        }
    }
}